// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-21 10:30
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.leetCode.isPrefixOfWord;

public class isPrefixOfWord {
    //  判断单词前缀
    public int isPrefixOfWords(String sentence, String searchWord) {
        // 单词是由空格隔开的，所以使用空格可以直接split出所有单词
        // 搜寻单词的长度为匹配项，当句子中单词长度小于搜寻单词长度，则表明搜寻单词不可能为该
        // 单词的前缀
        int len = searchWord.length();
        String[] words = sentence.split(" ");
        int wLen = words.length;
        for (int i = 0; i < wLen; i++) {
            int tLen = words[i].length();
            if (tLen >= len) {
                // 搜寻单词可能是当前单词的前缀
                String tmp = words[i].substring(0, len);
                if (searchWord.equals(tmp)) {
                    return i + 1;
                }
            }
        }
        return -1;
    }
}
